package com.zjl.Zookeeper.第03章_访问;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class C1_java连接基本操作 {
    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = getZooKeeper();
        System.out.println("--------------------------------------------------");

        Thread.sleep(1000000);
        // 最后，关闭ZooKeeper连接
        zooKeeper.close();
    }

    private static ZooKeeper getZooKeeper() throws IOException {
        // ZooKeeper服务器地址和端口  connectString的值不能有空格
        //String connectString = "82.157.71.243:2181,82.157.71.243:2181,82.157.71.243:2181";
//        String connectString = "82.157.71.243:2181"; // ZooKeeper服务器地址和端口
        String connectString = "127.0.0.1:2181"; // ZooKeeper服务器地址和端口
        int sessionTimeout = 2000; // 会话超时时间（毫秒）
        ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout,
                event -> {
                    // 这里可以处理WatchedEvent  如果在操作ZooKeeper时，设置了监听，则改动时会执行
                    System.out.println("Received event: " + event);
                }
        );
        return zooKeeper;
    }

    @Test//创建节点
    public void create() throws Exception {
        /**
         * List<ACL>   ---ZooDefs.Ids .
         * OPEN_ACL_UNSAFE:完全开放的ACL，任何连接的客户端都可以操作该属性znode
         * CREATOR_ALL_ACL:只有创建者才有ACL权限
         * READ_ACL_UNSAFE:只能读取ACL
         */
        /**
         * CreateMode.PERSISTENT
         *
         * PERSISTENT:持久性的节点，目录不会丢失
         * PERSISTENT_SEQUENTIAL:存储的数据不会丢失，并且根据当前已经存在的节点数目自动加1，然后返回给客户端已经成功创建的目录节点名
         * EPHEMERAL:临时目录节点，一旦创建这个节点的客户端与服务器端口也就是session超时，这种节点就会被删除
         * EPHEMERAL_SEQUENTIAL:临时自动编号节点，一旦创建这个节点的客户端与服务器端口也就是session超时，这种节点就会被删除
         */
        ZooKeeper zooKeeper = getZooKeeper();

        zooKeeper.create("/sg", "v3".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    }
    @Test//查询
    public void get() throws Exception {
        ZooKeeper zooKeeper = getZooKeeper();
        Stat stat = zooKeeper.exists("/sg",false);//获取当前节点的版本号
        byte[] data = zooKeeper.getData("/sg", false, stat);
        System.out.println(new String(data));
    }


    @Test//查询
    public void delete() throws Exception {
        ZooKeeper zooKeeper = getZooKeeper();
        Stat stat = zooKeeper.exists("/sg",false);//获取当前节点的版本号
        zooKeeper.delete("/sg",stat.getVersion());
    }

    @Test//修改
    public void set() throws Exception {
        ZooKeeper zooKeeper = getZooKeeper();
        Stat stat = zooKeeper.exists("/sg",false);//获取当前节点的版本号

        //由于高并发，所以 根据版本号修改
        Stat stat1 = zooKeeper.setData("/sg", "v4".getBytes(), stat.getVersion());
    }


    @Test//查询节点下的节点信息
    public void ls() throws Exception {
        ZooKeeper zooKeeper = getZooKeeper();
        //第一个参数:节点路径   第二参数:是否开启监听
        List<String> children = zooKeeper.getChildren("/", false);
        children.forEach(System.out::println);
    }

}
